//
//  QueueViewController.m
//  ObjectCProject
//
//  Created by lvfeijun on 2021/5/24.
//

#import "QueueViewController.h"

@interface QueueViewController ()

@property dispatch_queue_t myqueue;

@end

@implementation QueueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(NSArray *)getDatas{
    return @[@"dispatch_group_t",@"dispatch_suspend",@"groupSemaphore"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self tableViewSelcctedAction:indexPath];
}

#pragma mark dispatch_group_t+队列
//异步执行任务，同步等待全部任务执行完
-(void)dispatch_group_t{
    __block NSInteger number = 0;
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_t group = dispatch_group_create();
    
    //A耗时操作
    dispatch_group_enter(group);
    NSLog(@"A-");
    dispatch_group_async(group, queue, ^{
        number += 2222;
        NSLog(@"A");
        dispatch_group_leave(group);
        NSLog(@"A+");
    });
    
    //B网络请求
    dispatch_group_enter(group);
    NSLog(@"B-");
    dispatch_group_async(group, queue, ^{
        [self sendRequestWithCompletion:^(id response) {
            number += [response integerValue];
            NSLog(@"B");
            dispatch_group_leave(group);
            NSLog(@"B+");
        }];
    });
    
    //C网络请求
    dispatch_group_enter(group);
    NSLog(@"C-");
    dispatch_group_async(group, queue, ^{
        [self sendRequestWithCompletion:^(id response) {
            number += [response integerValue];
            NSLog(@"C");
            dispatch_group_leave(group);
            NSLog(@"C+");
        }];
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"%zd", number);
    });
}

- (void)sendRequestWithCompletion:(void (^)(id response))completion {
    //模拟一个网络请求
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) completion(@1111);
        });
    });
}

#pragma mark dispatch_suspend
//任务依次执行
- (void)dispatch_suspend {
    _myqueue = dispatch_queue_create("data.im.init", NULL);
    
    [self act1];
    [self act2];
    [self act3];
    
    NSLog(@"31231");
}
//每次都重新开启新的线程，而不是获取原有的线程。不可取
-(dispatch_queue_t)getQueueu{
    return dispatch_queue_create("data.im.init", NULL);
}
-(void)act1{
    dispatch_async(_myqueue, ^{
        dispatch_suspend(self->_myqueue);
        sleep(4);
        NSLog(@"1");
        dispatch_resume(self->_myqueue);
    });
}

-(void)act2{
    dispatch_async(_myqueue, ^{
        dispatch_suspend(self->_myqueue);
        NSLog(@"2");
        dispatch_resume(self->_myqueue);
    });
}

-(void)act3{
    dispatch_async(_myqueue, ^{
        dispatch_suspend(self->_myqueue);
        sleep(7);
        NSLog(@"4");
        dispatch_resume(self->_myqueue);
    });
}

#pragma mark group+semaphore
//异步执行任务，同步等待全部任务执行完
- (void)groupSemaphore{
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    dispatch_group_async(group, dispatch_queue_create("com.dispatch.test", DISPATCH_QUEUE_CONCURRENT), ^{
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.github.com"]];
        NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 请求完成，可以通知界面刷新界面等操作
            NSLog(@"第二步网络请求完成");
            dispatch_semaphore_signal(sema);
        }];
        [task resume];
        // 以下还要进行一些其他的耗时操作
        NSLog(@"耗时操作继续进行2");
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    });
    
    dispatch_semaphore_t sema1 = dispatch_semaphore_create(0);
    dispatch_group_async(group, dispatch_queue_create("com.dispatch.test", DISPATCH_QUEUE_CONCURRENT), ^{
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
        NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            // 请求完成，可以通知界面刷新界面等操作
            NSLog(@"第一步网络请求完成");
            dispatch_semaphore_signal(sema1);
        }];
        [task resume];
        // 以下还要进行一些其他的耗时操作
        NSLog(@"耗时操作继续进行1");
        dispatch_semaphore_wait(sema1, DISPATCH_TIME_FOREVER);
    });
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"刷新界面等在主线程的操作");
    });
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
